package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import org.codehaus.xfire.XFireFactory;
//import org.codehaus.xfire.client.XFireProxyFactory;
//import org.codehaus.xfire.service.binding.ObjectServiceFactory;

import org.bminfo.migle.wrapper.MiRandaWrapper;

public class MirandaServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public MirandaServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String miGLE_home = request.getSession().getAttribute("miGLE_home").toString();
		String folderName = request.getSession().getAttribute("folderName").toString();
		String go = request.getParameter("go");
		String ge = request.getParameter("ge");
		String en = request.getParameter("en");
		String sc = request.getParameter("sc");
//		String snp = request.getParameter("snp");
		String snp = "n";
		
//		org.codehaus.xfire.service.Service srvcModel = new ObjectServiceFactory().create(IMigleServer.class);
//		XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
//		String moopsServiceURL = "http://" + request.getServerName().toString() + ":"
//		+ request.getServerPort() + "/miGLE/services/MigleServer";
//		IMigleServer srvc = (IMigleServer)factory.create(srvcModel, moopsServiceURL);
		
		MiRandaWrapper miranda = new MiRandaWrapper();
		
		miranda.setMirandaPath(miGLE_home, folderName);
		miranda.miranda(go, ge, en, sc, snp);
		miranda.copyMigleFile();
		
//		System.out.println("Foldername in MirandaServlet is: " + folderName);
				
        RequestDispatcher rd = request.getRequestDispatcher("../temp/" + folderName + "/showmoops.jsp");   
        rd.forward(request, response); 
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
